#ifndef PROFILEMANAGER_H
#define PROFILEMANAGER_H

#include "network/networkhttprequest.h"
#include "data/userprofile.h"
#include "account/loginresponse.h"
#include "filetransfer/downloader.h"
#include "converse/converseabstractmessage.h"
#include "thread/insertprofilethread.h"
#include "thread/updatemessagestatusthread.h"

#include "event/createnewgroupevent.h"
#include "parser/createnewgroupparser.h"

#include <QTimer>
#include <QObject>

class GetOnlineFriendResquest;
class CreateNewGroupRequest;
class CreateNewGroupParser;
class CreateNewGroupEvent;

class ProfileManager : public QObject
{
    Q_OBJECT
public:
    explicit ProfileManager(QObject *parent = 0);
    static ProfileManager *instance(QObject *parent = 0);


    void init();
    void clearData();

    QList<UserProfile*> getListFriendProfile() const ;
    QList<UserProfile*> getRefreshListFriendProfile() const ;
    QList<UserProfile*> getSearchFriends(QString inputName) const ;
    QList<UserProfile*> getListFriendsProfileSortByName() const ;
    QList<UserProfile*> getListFriendsProfileSortByNameFirtTime() const ;


    UserProfile* getProfileById(const qint64 userId);
    void removeProfileFriend(const qint64 fid);

    void updateFriendProfile(Converse::AbstractMessage *message);
    void updateFriendProfile(const qint64 fid,
                             const int senderMsg,
                             const QString msgStatus,
                             const qint64 time);

    InsertProfileThread* insertProfileThread() const {
        return m_insertProfileThread;
    }

    UpdateMessageStatusThread* updateMsgThread() const {
        return m_updateMsgThread;
    }

    void addFriendToGroup(qint64 uid, qint64 fid);
    void deleteFriendFromGroup(qint64 uid, qint64 fid);
    void doCreateGroup(QString groupName);
    void createNewGroupEvent(const CreateNewGroupEvent &aCreateNewGroupEvent);

private:
    void setConnection();
    void sortListFriendAgain(qint64 fid);

signals:
    void getAllFriendProfileFinish();
    void getUnitFriendsProfile(QList<UserProfile*> aUnitFriends);
    void getAllFriendsOnline(QList<UserProfile*> aListFriendsOnline);
    void refreshCurrentListFriendSuccessfull();
    void startGetListFriendSortByName();
    void startGetListFriendSortByNameFirstTime();
    void createNewGroupSucessfull();

public:
    void doGetAllFriendIdRequest(const int page = 1);

private:

    void doGetAllProfileFriendRequest(const int idx , const int count = 30);
    void doGetProfileFinish();
    void doUpdateFriend2Local();
    void doGetAvatarAndUpdateFriend();
    void doGetUnitFriendsProfileFinish(const int idxFrom, const int idxTo);
    void doGetMessageStatusFirstTimeLogin(qint64 fid);
    void doGetOnlineFriendsRequest();

    UserProfile* getUserProfileByIdx( const int idx);

private slots:
    void onGetAllFriendIdResponsed(Network::HttpRequest *request);
    void onGetAllProfileFriendResponsed(Network::HttpRequest *request);
    void onGetMessageStatusResponsed(Network::HttpRequest *request);
    void onGetOnlineFriendsResponsed(Network::HttpRequest *request);
    void onUpdateAFriendFinish();
    void onOnlineFriendsRequestTimerTimeout();

private:
    static ProfileManager *m_instance;

    InsertProfileThread* m_insertProfileThread;
    UpdateMessageStatusThread* m_updateMsgThread;

    GetOnlineFriendResquest *m_onlineFriendRequest;
    CreateNewGroupRequest* m_createNewGroupRequest;

    QList<UserProfile*> _listFriendOnGroup;
    QList<UserProfile*> _listFriendProfile;
    QMap<qint64, UserProfile*> _mapFriendProfile;
    int _idxAvatarDownload;
    int _idxAvatarFrom;
    int _idxAvatarTo;
    QTimer *m_updateOnlineStatusTimer;

};

#endif // PROFILEMANAGER_H
